
var app = getApp();
var date = new Date();


//月

var M = (date.getMonth()
  + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);

//日

var D = date.getDate()
  < 10 ? '0' + date.getDate() :
  date.getDate();



var day = M + "月" + D +'日'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    surplusprice:0,
    originalPrice: 0,
    favorablePrice: 0,
    starttime:null,
    start:null,
    stop:null,
    stoptime:null,
    date:null,
    startsiteid:null,
    stopsiteid:null,
    clock: '',
    userInfo: {
      code: ''
    },
    line: {

    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // countdown(this);
    var userid = wx.getStorageSync("userid")
    console.log(options.id)
    console.log(userid)
    var that = this
    var favorablePrice = that.data.favorablePrice
    var originalPrice = that.data.originalPrice
    var surplusprice = that.data.surplusprice
    var startsiteid = that.data.startsiteid
    var stopsiteid = that.data.stopsiteid
    wx.request({
      url: app.globalData.url + '/actualRoute/actualNumOrder',
      data: { lineid: options.id, userId: userid },
      success(res) {
        console.log(res)
        if (res.data.favorablePrice == 0 && res.data.favorablePrice==''){
          that.setData({
            favorablePrice:"暂无",
            originalPrice: res.data.originalPrice,
            surplusprice: res.data.surplusprice
          })
        }
        else{
          that.setData({
            favorablePrice: res.data.favorablePrice,
            originalPrice: res.data.originalPrice,
            surplusprice: res.data.surplusprice
          })
        }

      }
    })
    

    this.setData({
      line: {
        id: options.id
      },
      stopsiteid: options.stopsiteid,
      startsiteid: options.startsiteid,
      date: day,
      starttime: options.starttime,
      start: options.start,
      stop: options.stop,
      stoptime: options.stoptime,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },
  access(res) {
var that = this
    that.shop();

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },
  shop() {
    var that = this;
    var stop = that.data.stopsiteid
    var start = that.data.startsiteid
    var userid = wx.getStorageSync("userid")
    var openid = that.data.userInfo.openid

    wx.request({
      url: app.globalData.url +'/wxpay/pay', //仅为示例，并非真实的接口地址
      data: {
        userId: userid,
        lineid: that.data.line.id,
        payNum: 1,
        gosite: start,
        endsite: stop
      },
      success(res) {
        console.log(res.data)
        if (res.data.order==-2){
          wx.showModal({
            title: "消息",
            content: res.data.msg,
            confirmText:"确定",
            confirmColor:'green',
            showCancel:false
          })
        }else{
        wx.setStorageSync("orderId", res.data.orderId)
        if (res && res.data) {
          wx.requestPayment({
            'timeStamp': res.data.timeStamp,
            'nonceStr': res.data.nonce_str,
            'package': 'prepay_id=' + res.data.prepay_id,
            'signType': 'MD5',
            'paySign': res.data.paySign,
            'success': function(res) {
  
              console.log(res)

              wx.showToast({
                title: '支付成功',
                icon:'success',
                duration:1400,
                success:function(){
                  // wx.request({
                  //   url: 'http://192.168.1.106:8080/wxpay/notify',
                  //   data:{},
                  //   success:function(res){
                  //     console.log(res.data)
                  //   }
                  // })
                  var orderId = wx.getStorageSync("orderId")
                  wx.redirectTo({
                    url: '../../pages/riding/riding?orderId=' + orderId,
                  })
                }
              })

            },
            'fail': function(res) {
              var orderId = wx.getStorageSync("orderId")
              wx.request({
                url: app.globalData.url +'/wxpay/cancelPay',
                data: { orderId: orderId},
                success:function(res){
                  console.log(res.data)
                }
              })
              console.log(res);
              wx.showToast({
                title: '支付失败',
                icon:'none',
                duration:1500
              })
wx.navigateBack({
  url: '../../pages/map/map',
})
            }
          })
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
          }
        }
      }
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function() {

  // }
})